using System;
using System.Reflection;
using System.IO;
using System.Xml;
using System.Collections;
using SharpObjects.Configuration;
using System.Web;
using SharpObjects.Configuration.Services;
using SharpObjects.Interfaces;
using SharpObjects.Configuration.ConfigurationProviders;

namespace SharpObjects.Logging.LogTargets 
{
    public class SafeTextFileLogTarget : FileLogTargetBase
    {
        #region CONSTRUCTORS

        public SafeTextFileLogTarget()
        {
		}

		public SafeTextFileLogTarget(string logDirectory)
            : base(logDirectory)
        {
		}

	    public SafeTextFileLogTarget(string logDirectory, string logFileNameBase)
            : base(logDirectory, logFileNameBase)
        {
		}

		public SafeTextFileLogTarget(string logDirectory, string logFileNameBase, string extension)
            : base(logDirectory, logFileNameBase, extension)
        {
		}

		public SafeTextFileLogTarget(string logDirectory, string logFileNameBase, string extension, int nMaxSize)
            : base(logDirectory, logFileNameBase, extension, nMaxSize)
        {
		}

        #endregion //CONSTRUCTORS


        public override void Release()
        {
        }


        /*
          <safe_text_log_target is_enabled="true">
            <formatter pathref="//logging/formatters/default_log_formatter" />
            <directory>Logs</directory>
            <file_name_base>SharpObjects.Logging_Test_SafeText</file_name_base>
            <file_extension>log</file_extension>
            <log_max_size units="kb">2048</log_max_size>
            <keep_history_for>30</keep_history_for>
            <origin_filter>
              <include>
                <starts_with>aaa</starts_with>
                <starts_with>bbb</starts_with>
                <contains>ccc</contains>
                <contains>ddd</contains>
                <ends_with>eee</ends_with>
                <ends_with>fff</ends_with>
              </include>
              <exclude>
                <starts_with>ggg</starts_with>
                <starts_with>hhh</starts_with>
                <contains>iii</contains>
                <contains>jjj</contains>
                <ends_with>kkk</ends_with>
                <ends_with>lll</ends_with>
              </exclude>
            </origin_filter>
          </safe_text_log_target>

        */

        public override void Initialize(XmlElement initializationElement)
        {
            base.Initialize(initializationElement);

            IConfigurationProvider init = new XmlConfigurationProvider(initializationElement);

            this.formatter = init.GetObject<ILogFormatter>(formatterTag, (ILogFormatter)null);

            if (this.formatter == null)
            {
                string formatterId = init.GetString(formatter_id_tag);
                this.formatter = ConfigurableServiceFactory.Instance.GetService<ILogFormatter>(formatterId);
            }
        }

        protected override void WriteToLog(ILogEntry logEntry)
        {
            try
            {
                if (File.Exists(this.logFileName))
                {
                    FileInfo fi = new FileInfo(this.logFileName);

                    if (fi.Length >= this.maxSize)
                    {
                        Rename();
                        CreateNew();
                        CleanupHistory();
                    }
                }
                else
                {
                    CreateNew();
                }

                using (TextWriter tw = File.AppendText(this.logFileName))
                {
                    string message = this.formatter.ApplyFormat(logEntry);
                    tw.Write(message);
                }
            }
            catch (Exception exception)
            {
                Log.AddErrorMessage(exception);
            }
        }

        protected override void Close()
        {
        }
    }
}
